-- ----------------------------------------------------------
--
-- Author: czf
-- date: 2018-12-14
-- description: 观察者模式-主题类（存放观察者）
--
-- ----------------------------------------------------------

Subject = Subject or BaseClass()

function Subject:__init()
	self.observers = {}
	self.state = nil
end

function Subject:GetState()
	return self.state
end

function Subject:SetState(state)
	self.state = state
	self:NotifyAllObservers()
end

-- 目前只是简单添加(其实若是更好扩展，采取key-value形式保存更好)
function Subject:Attach(observer)
	table.insert(self.observers, observer)
end

-- 广播
function Subject:NotifyAllObservers()
	for _, observer in ipairs(self.observers) do
		if observer.Update then
			observer:Update()
		end
	end
end